<?php
header("Content-Type: text/html; charset=utf-8");
require_once '../smarty/Smarty.class.php';
require_once '../lib/pagination.php';
require_once '../lib/query_railway.php';
require_once '../lib/base.php';
$user=$_SESSION["RU"];
//var_dump($user);
if(!$user)header("Location:../user/login.php"); 

$p=($_GET["p"]==null)|($_GET["p"]==0) ?1:$_GET["p"];
$PAGE_NUM=15;
$from=($p-1)*$PAGE_NUM;

$view=$_GET["v"];
$sp=htmlspecialchars( $_GET["sp"]);
$sz=htmlspecialchars( $_GET["sz"]);
$zz=htmlspecialchars($_GET["zz"]);
$zc=htmlspecialchars($_GET["zc"]);
$ze=htmlspecialchars($_GET["ze"]);
if($view>0){
	$str="$tb_start_border.border=$tb_border_end.border and $tb_start_border.box=$tb_border_end.box and $tb_start_border.start=$tb_start.en and $tb_border_end.end=$tb_end.en";
	if ($sp.$sz.$zz.$zc.$ze){
		if($sp)$str.=" and $tb_start.pv like '%$sp%'";
		if($sz)$str.=" and $tb_start.en like '%$sz%'";
		if($zz)$str.=" and $tb_start_border.border like '%$zz%'";
		if($zc)$str.=" and $tb_end.code like '%$zc%'";
		if($ze)$str.=" and $tb_end.en like '%$ze%'";
	}
	$dbtable ="$tb_start,$tb_end,$tb_start_border,$tb_border_end";
	
	$count=QR::get_total($dbtable, $str);//all_number
	//echo "<br/>".$count."<br/>";
	$field="$tb_start.cn as start_cn,$tb_start.en as start_en,$tb_start.pv as start_pv,$tb_start_border.box as start_box,$tb_start_border.stime as start_st,$tb_border_end.stime as end_st,
				 $tb_start_border.border as border,
				 $tb_end.code as end_code,$tb_end.en as end_en,$tb_end.cn as end_cn,$tb_end.ru as end_ru,$tb_end.cy as end_cy,
				 case when $tb_start_border.p20 = 0 OR $tb_border_end.p20 = 0 then 0 else $tb_start_border.p20+$tb_border_end.p20 end as p20,
				 case when $tb_start_border.p40gp = 0 OR $tb_border_end.p40gp = 0 then 0 else $tb_start_border.p40gp+$tb_border_end.p40gp end as p40gp,
				 case when $tb_start_border.p40hc = 0 OR $tb_border_end.p40hc = 0 then 0 else $tb_start_border.p40hc+$tb_border_end.p40hc end as p40hc,
				 concat($tb_start_border.remark,'<br/>',$tb_border_end.remark) as remark";
	$str.=" order by $tb_start.en";
	$data=QR::get_line($dbtable, $field,$str,$from,$PAGE_NUM);//all_data
	//var_dump($data);
}
$start=QR::get_field($tb_start,"en");
$tran=QR::get_field($tb_border,"cn");
$end=QR::get_field($tb_end,"en");
$start_p=QR::get_field($tb_start,"pv");

$smarty = new Smarty;
$smarty->assign("userdata",$data);
$smarty->assign("pagination", getPageNavigation($p,$count,$PAGE_NUM));
$smarty->assign("start",json_encode($start));
$smarty->assign("tran",$tran);
$smarty->assign("end",json_encode($end));
$smarty->assign("start_p",json_encode($start_p));
$smarty->assign("view",$view);
$smarty->assign("sp",$sp);
$smarty->assign("sz",$sz);
$smarty->assign("zz",$zz);
$smarty->assign("zc",$zc);
$smarty->assign("ze",$ze);
$smarty->assign("user",$user);
$smarty->display('railway_list.tpl');
function get_st($st,$et){
	$st=strtotime($st);
	$et=strtotime($et);
	if($st>$et) return date('Y-m-d', $et);
	return date('Y-m-d', $st);
}
?>